package com.birdy.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

/**
 * @author birdy
 * @date 2022/3/30 12:22 PM
 */
@Aspect
@Component
@Slf4j
public class LogTimeUsedAspect {

    @Pointcut("@annotation(com.birdy.annotation.LogTimeUsed)")
    public void pointCut() {}

    @Around("pointCut()")
    public Object time(ProceedingJoinPoint joinPoint) {
        Object object;
        long start = System.currentTimeMillis();
        try {
            object = joinPoint.proceed();
        } catch (Throwable e) {
            e.printStackTrace();
            return null;
        }
        long used = System.currentTimeMillis() - start;
        log.info("【{}】接口花费时长：{}ms", ((MethodSignature)joinPoint.getSignature()).getMethod().getName(), used);
        return object;
    }
}
